import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";

// 定义初始状态值
const initialState = {
  count: 0,  // 默认值
  price: 10.5,
  sum: 0,
  channelList:[]
};

const fetchChannls = createAsyncThunk('counter/fetchChannels',async()=>{
  let res = await axios.get('http://blogapi.shbwyz.com/article/list')
  return res.data.rows
})

const counterSilce = createSlice({
  name: 'counter', 
  initialState, 
  reducers: { 
    increment(state, { payload }) {
      state.count += payload
      state.sum = state.count * state.price
    },
    decrement(state) {
      state.count -= 1
    }
  },
  extraReducers:(builder)=>{
    builder
    .addCase(fetchChannls.pending,(state)=>{

    })
    .addCase(fetchChannls.fulfilled,(state,{payload})=>{
      state.channelList = payload
    })
    .addCase(fetchChannls.rejected,(state)=>{
      console.log(state,'请求失败')
    })
  }
})

export {fetchChannls}
export const { increment, decrement, setChannerList} = counterSilce.actions
export default counterSilce.reducer